import {applyMiddleware, compose, createStore} from 'redux'
import thunk from 'redux-thunk'
import {hashHistory} from 'react-router'
import makeRootReducer from '../reducers/index'
import {updateLocation} from '../reducers/location'
import DevTools from '../containers/DevTools'

export default function configureStore(initialState = {}) {
  // ======================================================
  // Middleware Configuration
  // ======================================================
  const middleware = [thunk]

  // ======================================================
  // Store Enhancers
  // ======================================================
  // Const enhancers = []
  //   Const devToolsExtension = window.devToolsExtension
  //   If (typeof devToolsExtension === 'function') {
  //     Enhancers.push(devToolsExtension())
  //   }

  // ======================================================
  // Store Instantiation and HMR Setup
  // ======================================================
  const store = createStore(
    makeRootReducer(),
    initialState,
    compose(
      applyMiddleware(...middleware),
      DevTools.instrument()
    )
  )

  store.asyncReducers = {}

  // To unsubscribe, invoke `store.unsubscribeHistory()` anytime
  store.unsubscribeHistory = hashHistory.listen(updateLocation(store))

  if (module.hot) {
    module.hot.accept('../reducers', () => {
      const reducers = require('../reducers').default

      store.replaceReducer(reducers(store.asyncReducers))
    })
  }

  return store
}
